<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组织ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入组织ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入设备代码/位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警类型" prop="alarmType">
        <el-select v-model="queryParams.alarmType" placeholder="请选择告警类型" clearable style="width:120px">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.id"
            :label="dict.alarmName"
            :value="dict.alarmCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="告警时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeAlarmTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="处置状态" prop="handleStatus">
        <el-select v-model="queryParams.handleStatus" placeholder="请选择处置状态" clearable style="width:120px">
          <el-option
            v-for="dict in handle_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处置结论" prop="handleResult">
        <el-select v-model="queryParams.handleResult" placeholder="请选择处置结论" clearable style="width:120px">
          <el-option
            v-for="dict in handle_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:warning:remove']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['alarm:warning:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warningList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="组织ID" align="center" prop="deptId" />
      <el-table-column label="设备编码" align="center" prop="deviceCode" />
      <el-table-column label="设备位置" align="center" prop="position" />
      <el-table-column label="告警类型" align="center" prop="alarmTypeName" />
      <el-table-column label="告警时间" align="center" prop="alarmTime" width="180" />
      <el-table-column label="撤销时间" align="center" prop="repealTime" width="180" />
      <el-table-column label="告警图片" align="center" prop="imagePath" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imagePath" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="告警说明" align="center" prop="comment" />
      <el-table-column label="处置状态" align="center" prop="handleStatus">
        <template #default="scope">
          <dict-tag :options="handle_status" :value="scope.row.handleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="处置结论" align="center" prop="handleResult">
        <template #default="scope">
          <dict-tag :options="handle_result" :value="scope.row.handleResult"/>
        </template>
      </el-table-column>
      <el-table-column label="处置时间" align="center" prop="handleTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处置人" align="center" prop="handleUserName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" v-if="scope.row.handleStatus != '0'" @click="handleView(scope.row)">查看</el-button>
          <el-button link type="primary" icon="Edit" v-else @click="handleUpdate(scope.row)">处置</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['alarm:warning:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改告警记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="warningRef" :model="form" :rules="rules" label-width="80px" disabled>
        <el-form-item label="组织ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入组织ID" />
        </el-form-item>
        <el-form-item label="设备ID" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="设备分类" prop="deviceTypeId">
          <el-input v-model="form.deviceTypeId" placeholder="请输入设备分类" />
        </el-form-item>
        <el-form-item label="设备分类" prop="deviceSysId">
          <el-input v-model="form.deviceSysId" placeholder="请输入设备分类" />
        </el-form-item>
        <el-form-item label="告警时间" prop="alarmTime">
          <el-date-picker clearable
            v-model="form.alarmTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择告警时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="告警图片" prop="imagePath">
          <image-upload v-model="form.imagePath"/>
        </el-form-item>
        <el-form-item label="告警视频" prop="videoPath">
          <el-input v-model="form.videoPath" placeholder="请输入告警视频" />
        </el-form-item>
        <el-form-item label="设备编码" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="事件代码" prop="eventCode">
          <el-input v-model="form.eventCode" placeholder="请输入事件代码" />
        </el-form-item>
        <el-form-item label="设备位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入设备位置" />
        </el-form-item>
        <el-form-item label="告警说明" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入告警说明" />
        </el-form-item>
        <el-form-item label="处置状态" prop="handleStatus">
          <el-radio-group v-model="form.handleStatus">
            <el-radio
              v-for="dict in handle_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处置结论" prop="handleResult">
          <el-select v-model="form.handleResult" placeholder="请选择处置结论">
            <el-option
              v-for="dict in handle_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处置人ID" prop="handleTime">
          <el-date-picker clearable
            v-model="form.handleTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择处置人ID">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处置人" prop="handleUserId">
          <el-input v-model="form.handleUserId" placeholder="请输入处置人" />
        </el-form-item>
        <el-form-item label="处置人姓名" prop="handleUserName">
          <el-input v-model="form.handleUserName" placeholder="请输入处置人姓名" />
        </el-form-item>
        <el-form-item label="处置说明" prop="handleRemark">
          <el-input v-model="form.handleRemark" placeholder="请输入处置说明" />
        </el-form-item>
        <el-form-item label="外部告警代码" prop="alarmCode">
          <el-input v-model="form.alarmCode" placeholder="请输入外部告警代码" />
        </el-form-item>
        <el-form-item label="撤销时间" prop="repealTime">
          <el-date-picker clearable
            v-model="form.repealTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择撤销时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>

<script setup name="Warning">
import { listWarning, getWarning, delWarning, addWarning, updateWarning } from "@/api/alarm/warning";
import { selectType } from "@/api/alarm/type";

const { proxy } = getCurrentInstance();
const { handle_result, handle_status } = proxy.useDict('handle_result', 'handle_status');

const warningList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeAlarmTime = ref([]);
const typeOptions = ref([]);


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'desc',
    deptId: null,
    deviceId: null,
    deviceTypeId: null,
    deviceSysId: null,
    alarmType: null,
    alarmTime: null,
    imagePath: null,
    videoPath: null,
    deviceCode: null,
    eventCode: null,
    position: null,
    comment: null,
    handleStatus: null,
    handleResult: null,
    handleTime: null,
    handleUserId: null,
    handleUserName: null,
    handleRemark: null,
    alarmCode: null,
    repealTime: null
  },
  rules: {
    deptId: [
      { required: true, message: "组织ID不能为空", trigger: "blur" }
    ],
    deviceId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    deviceTypeId: [
      { required: true, message: "设备分类不能为空", trigger: "blur" }
    ],
    deviceSysId: [
      { required: true, message: "设备分类不能为空", trigger: "blur" }
    ],
    alarmType: [
      { required: true, message: "事件类型不能为空", trigger: "change" }
    ],
    alarmTime: [
      { required: true, message: "告警时间不能为空", trigger: "blur" }
    ],
    deviceCode: [
      { required: true, message: "设备编码不能为空", trigger: "blur" }
    ],
    eventCode: [
      { required: true, message: "事件代码不能为空", trigger: "blur" }
    ],
    position: [
      { required: true, message: "设备位置不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询告警记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeAlarmTime && '' != daterangeAlarmTime) {
    queryParams.value.params["beginAlarmTime"] = daterangeAlarmTime.value[0];
    queryParams.value.params["endAlarmTime"] = daterangeAlarmTime.value[1];
  }
  listWarning(queryParams.value).then(response => {
    warningList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: null,
    deviceId: null,
    deviceTypeId: null,
    deviceSysId: null,
    alarmType: null,
    alarmTime: null,
    imagePath: null,
    videoPath: null,
    deviceCode: null,
    eventCode: null,
    position: null,
    comment: null,
    handleStatus: null,
    handleResult: null,
    handleTime: null,
    handleUserId: null,
    handleUserName: null,
    handleRemark: null,
    alarmCode: null,
    repealTime: null
  };
  proxy.resetForm("warningRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeAlarmTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加告警记录";
}

/** 修改按钮操作 */
function handleEdit(row) {
  reset();
  const _id = row.id || ids.value
  getWarning(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "处置告警";
  });
}
function handleView(row) {
  reset();
  const _id = row.id || ids.value
  getWarning(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "查看告警记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["warningRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarning(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarning(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除选中的告警记录数据项？').then(function() {
    return delWarning(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('alarm/warning/export', {
    ...queryParams.value
  }, `warning_${new Date().getTime()}.xlsx`)
}


getList();
selectType().then(response=>{
  typeOptions.value = response.rows;
})
</script>
